#include "byte_code.h"

string ByteCodeHelper::GetNameByByteCode(BYTE_CODE code)
{
    switch(code) {
        case BYTE_CODE::MOV_CON:
            return MOV_CON_NAME;
        case BYTE_CODE::MOV:
            return MOV_NAME;
        case BYTE_CODE::ADD:
            return ADD_NAME;
        case BYTE_CODE::MINUS:
            return MINUS_NAME;
        case BYTE_CODE::MUL:
            return MUL_NAME;
        case BYTE_CODE::DIV:
            return DIV_NAME;
        case BYTE_CODE::PRINT:
            return PRINT_NAME;
        default:
            cout << TAG << "Unknow byte code " << static_cast<int>(code) << endl;
            return UNKNOWN_NAME;
    }
}

BYTE_CODE ByteCodeHelper::GetByteCodeByName(string name)
{
    if (name == MOV_CON_NAME) {
        return BYTE_CODE::MOV_CON;
    } else if (name == MOV_NAME) {
        return BYTE_CODE::MOV;
    } else if (name == ADD_NAME) {
        return BYTE_CODE::ADD;
    } else if (name == MINUS_NAME) {
        return BYTE_CODE::MINUS;
    } else if(name == MUL_NAME) {
        return BYTE_CODE::MUL;
    } else if (name == DIV_NAME) {
        return BYTE_CODE::DIV;
    } else if (name == PRINT_NAME) {
        return BYTE_CODE::PRINT;
    } else {
        cout << TAG << "Unknow byte code name " << name << endl;
    }
}

string ByteCode::GetCode()
{
    ostringstream oss;
    if (byte_code_ == BYTE_CODE::MOV_CON) {
        oss << ByteCodeHelper::GetNameByByteCode(byte_code_) << " v" << target_ << " #" << source_ << endl;
    } else if (byte_code_ == BYTE_CODE::PRINT) {
        oss << ByteCodeHelper::GetNameByByteCode(byte_code_) << " v" << target_ << " 00" << endl;
    } else {
        oss << ByteCodeHelper::GetNameByByteCode(byte_code_) << " v" << target_ << " v" << source_ << endl;
    }
    return oss.str();
}